package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.BookingRecord;
import org.example.service.BookingRecordService;
import org.example.utils.ResponseUtil.ResponseResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

import static org.example.utils.ResponseUtil.StatusCode.DATA_NOT_EXIST;

@Slf4j
@RestController
@RequestMapping("/booking")
public class BookingRecordController {

    @Resource
    private BookingRecordService bookingRecordService;


    /**
     * 增加 疫苗预约记录
     */
    @RequestMapping("/insertBookingRecord")
    public ResponseResult insertBookingRecord(@RequestBody BookingRecord record){
        System.out.println("执行了insertBookingRecord方法。"+record);
        //如果添加的数据存在空值
        if (record==null|| record.getUser_uid()==null || record.getBooking_time()==null
                || record.getUser_uid() == 0 || record.getBooking_time().equals("") ){
            return new ResponseResult(DATA_NOT_EXIST, "预约信息没有填写完整，预约失败！");
        }
        return  bookingRecordService.insertBookingRecord(record);
    }




    /**
     * 修改 记录：根据疫苗预约记录的id
     */
    @RequestMapping("/updateBookingRecord")
    public ResponseResult updateBookingRecordById(@RequestBody BookingRecord record){
        System.out.println("执行了updateBookingRecordById方法。"+record);
        return bookingRecordService.updateBookingRecordById(record);
    }



    /**
     * 物理删除：根据疫苗预约记录表的br_id
     */
    @RequestMapping("/deleteBookingRecordById/{id}")
    public ResponseResult deleteBookingRecordById(@PathVariable("id") Integer id){
        System.out.println("执行了deleteBookingRecordById方法。id="+id);
        return bookingRecordService.deleteBookingRecordById(id);
    }


    /**
     * 根据用户的user_uid，查找多个预约记录
     */
    @RequestMapping("/selectBookingRecordByUserId/{userId}")
    public ResponseResult selectBookingRecordByUserId(@PathVariable("userId") Integer userId){
        System.out.println("执行了selectBookingRecordByUserId方法。userId="+userId);
        return  bookingRecordService.selectBookingRecordByUserId(userId);
    }


    /**
     * 根据预约记录表的br_id，查找唯一的对象
     */
    @RequestMapping("/selectBookingRecordById")
    public ResponseResult selectBookingRecordById(@RequestBody BookingRecord record){
        System.out.println("执行了selectBookingRecordById方法。"+record);
        if (record==null|| record.getBr_id()==null|| record.getBr_id()==0 ){
            return new ResponseResult(DATA_NOT_EXIST, "疫苗预约记录的id为空,请填写！");
        }
        return  bookingRecordService.selectBookingRecordById(record.getBr_id());
    }




        /**
         * 根据某一个查询条件，查找疫苗预约记录
         */
    @RequestMapping("/selectBookingRecordByArg")
    public ResponseResult selectBookingRecordByArg(@RequestBody Map<String,Object> map){
        System.out.println("执行了selectBookingRecordByArg方法！");
        if (map == null || map.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST, "查询条件为空,请填写！");
        }

        //获取map中的查询条件和待匹配的值
        for (Map.Entry<String,Object> entry:map.entrySet()){
            if (entry.getKey()==null || entry.getKey().equals("") ||
                    entry.getValue()==null || entry.getValue().equals("")){
                return new ResponseResult(DATA_NOT_EXIST,"查询失败，查询条件为空！");
            }

            String key = entry.getKey();//获取key，即查询条件
            String  value1;  //用于存储 String类型的待匹配值
            Integer value2;  //用于存储 Integer类型的待匹配值


            //根据用户的userId精确匹配
            if ("userId".equals(key)){
                value2 = Integer.parseInt( (String) entry.getValue() ) ;
                System.out.println("查询条件：key="+key+"，\t值value="+value2);
                return bookingRecordService.selectBookingRecordByUserId(value2);
            }
            //模糊查询userAccount
            else if ("userAccount".equals(key)){
                value1 = (String) entry.getValue();
                System.out.println("查询条件：key="+key+"，\t值value="+value1);
                return bookingRecordService.selectBookingRecordLikeUserAccount(value1);
            }
            //模糊查询username
            else if ("username".equals(key)){
                value1 = (String) entry.getValue();
                System.out.println("查询条件：key="+key+"，\t值value="+value1);
                return bookingRecordService.selectBookingRecordLikeUsername(value1);
            }
            //根据预约时间进行查询
            else if ("bookingDate".equals(key)){
                if (entry.getValue().toString().length()<10){
                    return new ResponseResult(DATA_NOT_EXIST,"查询失败，日期输入有误！");
                }
                value1 = entry.getValue().toString().substring(0,10);
                System.out.println("查询条件：key="+key+"，\t值value="+value1);
                return bookingRecordService.selectBookingRecordLikeBooking_time(value1);
            }
        }

        return new ResponseResult(DATA_NOT_EXIST,"查询失败，查询条件有误！");
    }


    /**
     * 根据预约时间升序查询 所有的预约记录
     */
    @RequestMapping("/AscSelectAllByTime")
    public ResponseResult AscSelectAllByTime(){
        System.out.println("执行了AscSelectAllByTime方法。");
        return bookingRecordService.AscSelectAllByTime();
    }


    /**
     * 根据预约时间降序查询 所有的预约记录
     */
    @RequestMapping("/DescSelectAllByTime")
    public ResponseResult DescSelectAllByTime(){
        System.out.println("执行了DescSelectAllByTime方法。");
        return bookingRecordService.DescSelectAllByTime();
    }


    /**
     * 查询所有疫苗预约记录
     */
    @RequestMapping("/selectAllBookingRecord")
    public ResponseResult selectAllBookingRecord(){
        System.out.println("执行了selectAllBookingRecord方法。");
        return bookingRecordService.selectAllBookingRecord();
    }

}
